package com.syher.seconds.kill.provider.service.impl;

import com.alibaba.dubbo.rpc.service.GenericException;
import com.alibaba.dubbo.rpc.service.GenericService;
import com.google.common.collect.Lists;
import com.syher.seconds.kill.pojo.BasePojo;
import com.syher.seconds.kill.provider.core.mybatis.BasicMapper;
import com.syher.seconds.kill.service.IBaseService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.common.Mapper;

import java.util.Iterator;
import java.util.List;

public abstract class BaseServiceImpl<T extends BasePojo, M extends BasicMapper<T>> implements IBaseService<T>, Mapper<T>, GenericService{
    private Class mapperInterface;
    
    @Autowired(required = true)
    private List<BasicMapper<T>> mapper;

    public BaseServiceImpl() {
        Class[] interfaces = this.getClass().getInterfaces();
        this.mapperInterface = Lists.newArrayList(interfaces)
                .stream().filter(clazz -> BasicMapper.class.isAssignableFrom(clazz))
                .findFirst().get();
    }

    public M getMapper() {
        if (mapperInterface == null) return null;

        Iterator iter = this.mapper.iterator();
        BasicMapper mapper;
        do {
            if (!iter.hasNext()) return null;
            mapper = (BasicMapper) iter.next();
        } while (!this.mapperInterface.isAssignableFrom(mapper.getClass()));

        return (M) mapper;
    }

    @Override
    public Object $invoke(String s, String[] strings, Object[] objects) throws GenericException {
        return null;
    }

    @Override
    public int addBatch(List<T> var1) throws Exception {
        return 0;
    }

    @Override
    public int deleteByIds(String var1) throws Exception {
        return 0;
    }

    @Override
    public int deleteByPrimaryKey(Object key) {
        return 0;
    }

    @Override
    public int delete(T record) {
        return 0;
    }

    @Override
    public int insert(T record) {
        return getMapper().insert(record);
    }

    @Override
    public int insertSelective(T record) {
        return getMapper().insertSelective(record);
    }

    @Override
    public List<T> selectAll() {
        return null;
    }

    @Override
    public T selectByPrimaryKey(Object key) {
        return getMapper().selectByPrimaryKey(key);
    }

    @Override
    public int selectCount(T record) {
        return 0;
    }

    @Override
    public List<T> select(T record) {
        return null;
    }

    @Override
    public T selectOne(T record) {
        return null;
    }

    @Override
    public int updateByPrimaryKey(T record) {
        return getMapper().updateByPrimaryKey(record);
    }

    @Override
    public int updateByPrimaryKeySelective(T record) {
        return getMapper().updateByPrimaryKeySelective(record);
    }

    @Override
    public int deleteByExample(Object example) {
        return 0;
    }

    @Override
    public List<T> selectByExample(Object example) {
        return null;
    }

    @Override
    public int selectCountByExample(Object example) {
        return 0;
    }

    @Override
    public int updateByExample(T record, Object example) {
        return 0;
    }

    @Override
    public int updateByExampleSelective(T record, Object example) {
        return 0;
    }

    @Override
    public List<T> selectByExampleAndRowBounds(Object example, RowBounds rowBounds) {
        return null;
    }

    @Override
    public List<T> selectByRowBounds(T record, RowBounds rowBounds) {
        return null;
    }
}
